$(function(){
      
    var idRotina = $('#idRotina').val();
    var lastSelection
    jQuery("#gridResultado").jqGrid({
        caption: "Alteração de Resultado",
        url: "intranet/Resultado/grid/id/"+idRotina,
        datatype: "json",
        colNames:['Exame', 'Tipo Resultado', 'Metodologia', 'Kit Sorológico'],
        colModel:[
            {name:'nome', index:'nome', width:180, align:"center"},
            {name:'resultado', index: 'resultado', width: 105, align: 'center', search:false},
            {name:'metodologia', index:'metodologia', width:300, align:"center", search:false, editable:false},
            {name:'kitSorologia', index:'kitSorologia', width:345, align:"center", search:false, editable:false}
        ],
        //rowNum: 10,
        //rowList: [10,20,30,50,100],
        width: "100%",
        height: 250,//***********************************
        pager: '#navResultado',
        sortname: "idExame",
        sortorder: "ASC",
        multiselect: false,
        viewrecords: true,
        loadonce:true,
        scroll:true,//************************************
        ignoreCase:true,
        subGrid: true,
        subGridRowExpanded: function(subgrid_id, row_id) {
            var subgrid_table_id;
            var pager_id;
            subgrid_table_id = subgrid_id+"_t";
            pager_id = "p_"+subgrid_table_id;
            $("#"+subgrid_id).html("<table id='"+subgrid_table_id+"' class='scroll'></table><div id='"+pager_id+"' class='scroll'></div>");
            //em url row_id id da linha do grid (idExame)
            //em edit url id da linha do subgrid (idResultado)
            jQuery("#"+subgrid_table_id).jqGrid({
                url:'intranet/Resultado/subgrid/row_id/'+row_id+'/idRotina/'+idRotina,
                editurl: "intranet/resultado/editar/idResultado/"+row_id,
                datatype: 'json',
                colNames: ['Amostra', 'Inicial','Resposta', 'Valor 1', 'Valor 2'],
                colModel: [
                    {name: 'idAmostra', index: 'idAmostra', width: 150, align: 'center'},
                    {name: 'inicial', index: 'inicial', width: 100, align: 'center'},
                    {name: 'resposta', index: 'resposta', width: 180, align: 'center', editable:true,
                        edittype:"select", editrules:{required:true}, 
                        editoptions: {value:'N:NÃO REAGENTE;P:REAGENTE;I:INDETERMINADO;R:REPETIÇÃO'}},
                    {name: 'valor1', index: 'valor1', width: 220, align: 'center', search:false,
                        editable:true, editrules:{number:true}},
                    {name: 'valor2', index: 'valor2', width: 220, align: 'center', search:false,
                        editable:true, editrules:{number:true}}
                ],
                //rowNum: 10,
                pager: pager_id,
                height: "100%",
                sortname: "idAmostra",
                sortorder: "ASC",
                //rowList: [10,20,30,50,100],
                viewrecords: true,
                scroll:true,
                loadonce:true,
                onSelectRow: function(id){
                    if(id && id!==lastSelection){
                        var grid = $(this);
                        grid.restoreRow(lastSelection);
                        grid.editRow(id, true);
                        lastSelection=id;
                    }
                }
            });
            jQuery("#"+subgrid_table_id).jqGrid('navGrid',"#"+pager_id,{edit:false,add:false,del:false,search:false,refresh:false})
            jQuery("#"+subgrid_table_id).jqGrid('filterToolbar',{stringResult:true, searchOnEnter:false});
        }
    })
    .navGrid("#navResultado",{
        edit: false, //Ativar/Desativar bot�o de Edi��o
        add: false, //Ativar/Desativar bot�o de Adi��o
        del: false, //Ativar/Desativar bot�o delete
        search: false, //Ativar/Desativar bot�o de busca
        view: false, //Ativar/Desativar bot�o de visualiza��o dos registros
        refresh: false //Ativar/Desativar bot�o refresh
    })
    .navButtonAdd('#navResultado',{
        caption:"Voltar", 
        //buttonicon:"ui-icon-add", 
        onClickButton: function(){ 
            intranet.carregaModulo('intranet/resultado/index/');
        }, 
        position:"last"
    });
});
jQuery("#gridResultado").jqGrid('navGrid','#navResultado',{edit:false,add:false,del:false,search:false});
jQuery("#gridResultado").jqGrid('filterToolbar',{stringResult:true, searchOnEnter:false});